package com.cfs.view;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.cfs.custom.RedisRepo;
import com.cfs.custom.enums.EnumAccountSrc;
import com.cfs.entity.school.Classs;
import com.cfs.entity.sys.User;
import com.cfs.service.common.CommonService;
import com.cfs.service.sys.SchoolService;
import com.cfs.service.sys.ShiroService;
import com.cfs.service.sys.UserService;
import com.cfs.utils.ShiroUtils;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * Created by Administrator on 2017/9/25/025.
 */
@RestController
public class IndexController {

    @Autowired
    private UserService userService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private RedisRepo redisRepo;
    @Autowired
    private ShiroService shiroService;

    @ResponseBody
    @RequestMapping(value = "index")
    public String index(){
        return "success index";
    }

    @RequestMapping(value = "userList")
    public Object userList(Long id,Integer pageNum){
        PageHelper.startPage(pageNum, 3);
        EntityWrapper<User> wrapper = new EntityWrapper();
        if(id != null){
            wrapper.eq("id", id);
        }
        List<User> users = userService.getUserMapper().selectList(wrapper);
        return users;
    }

    @RequestMapping(value = "add/user")
    public Object addUser(){
        User user = new User();
        user.setAccount("10000");
        user.setAddress("甘肃省");
        user.setAge(17);
        user.setCreateTime(new Date());
        user.setNickname("行走的蜗牛");
        user.setUsername("张林");
        int is = userService.getUserMapper().insert(user);
        System.out.println(is);
        return "";
    }

    @RequestMapping(value = "m/list")
    public Object mUserList(Long id){
        return  userService.selectByAuto(id);
    }



    @RequestMapping(value = "class/list")
    public Object classList(){
        PageHelper.startPage(1,10);
        EntityWrapper<Classs> wrapper = new EntityWrapper<>();
        wrapper.orderBy("id", false);
        return schoolService.getClasssMapper().selectList(wrapper);
    }

    @RequestMapping(value = "coms/list")
    public Object comsList(){
        return commonService.getCommonMapper().selectComByPlan(591);
    }


    @RequestMapping(value = "dclass/list")
    public Object dclassList(Integer pageNum){
        PageHelper.startPage(pageNum,10);
        return null;
    }


    @RequestMapping(value = "save/classs")
    public Object saveClasss(){
        Classs cls = new Classs();
        cls.setAcademeId(1L);
        cls.setCreateTime(new Date());
        cls.setEntryYear("2013");
        cls.setName("班级");
        cls.setSrc(EnumAccountSrc.ADMIN_ADD);
        cls.setPersonCount(12);
        cls.setPracticeCount(12);
        cls.setAcademy("学院");
        cls.setUserId(1L);
        cls.setSchoolId(1L);
        schoolService.getClasssMapper().insert(cls);
        return "success";
    }




    @ApiOperation(value = "获取班级列表",httpMethod = "POST", notes = "这是一个测试获取班级的信息API", tags = "测试API")
    @ApiImplicitParam(name = "pageNum",required = true,paramType = "query",value = "第几页")
    @RequestMapping(value = "au/list")
    public Object classList(@RequestParam  Integer pageNum){
        PageHelper.startPage(pageNum,20);
        ShiroUtils.logout();
        EntityWrapper<Classs> wrapper = new EntityWrapper<>();
        wrapper.orderBy("id", false);
        return schoolService.getClasssMapper().selectList(wrapper);
    }


    @RequestMapping(value = "redis/put")
    public Object redisPut(){
        redisRepo.setEx("你哈o", "我是中国人", 1, TimeUnit.HOURS);
        return redisRepo.get("你哈o");
    }

    
    @RequestMapping("update/per")
    public String updatePer() {
    	if(ShiroUtils.hasRole("SYS")) {
    		shiroService.updatePermission();
    	}
    	return "success";
    }
    
}
